/**
 * Copyright (c) 2019 AsHacker All rights reserved.
 *
 * http://www.ashacker.com
 *
 * 版权所有，侵权必究！
 */

package io.renren.modules.app.controller;

import com.alibaba.fastjson.JSON;
import io.renren.common.utils.R;
import io.renren.modules.app.annotation.Login;
import io.renren.modules.lucky.entity.AppUserAddrEntity;
import io.renren.modules.lucky.entity.AppUserInfoEntity;
import io.renren.modules.lucky.entity.AppUserScoreEntity;
import io.renren.modules.lucky.service.AppUserAddrService;
import io.renren.modules.lucky.service.AppUserInfoService;
import io.renren.modules.lucky.service.AppUserScoreService;
import io.renren.modules.sys.controller.AbstractController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * APP用户信息
 *
 * @author Chen chenchen@ashacker.com
 */
@RestController
@RequestMapping("/app")
@Api("APP用户信息")
public class AppUserController extends AbstractController {

	@Autowired
	private AppUserInfoService userInfoService;

	@Autowired
	private AppUserAddrService userAddrService;

	@Autowired
	private AppUserScoreService userScoreService;

	/**
	 * 获取用户信息
	 */
	@Login
	@GetMapping("getUserInfo")
	@ApiOperation("用户信息")
	public R getUserInfo(@RequestParam String user_id, @RequestParam String token) {

		logger.info("getUserInfo方法执行开始：传入参数为   "+user_id+"---"+token);

		String userId = user_id==null?"":user_id;
		String userToken = token==null?"":token;

		// 用户信息
		Map<String, Object> map = userInfoService.queryUserInfo(userId, userToken);

		logger.info("getUserInfo方法执行结束：返回值为   "+ JSON.toJSONString(map));

		return R.ok(map);
	}

	/**
	 * 保存用户信息
	 */
	@Login
	@PostMapping("saveUserInfo")
	@ApiOperation("保存用户信息")
	public R saveUserInfo(@RequestBody AppUserInfoEntity userInfoEntity, @RequestParam String token) {

		logger.info("saveUserInfo方法执行开始：传入参数为   "+JSON.toJSONString(userInfoEntity)+"---"+token);

		String userToken = token==null?"":token;

		// 保存用户信息
		int num = userInfoService.saveUserInfo(userInfoEntity, userToken);

		Map<String, Object> map = new HashMap<>();
		map.put("num", num);

		logger.info("saveUserInfo方法执行结束：返回值为   "+ JSON.toJSONString(map));

		return R.ok(map);
	}

	/**
	 * 获取用户地址列表
	 */
	@Login
	@GetMapping("getUserAddrList")
	@ApiOperation("用户地址列表")
	public R getUserAddrList(@RequestParam String page_start, @RequestParam String page_size, @RequestParam String token,
							 @RequestParam String user_id) {

		logger.info("getUserAddrList方法执行开始：传入参数为   "+page_start+"---"+page_size+"---"+token+"---"+user_id);

		String userToken = token==null?"":token;
		String userId = token==null?"":user_id;

		// 用户地址
		List<AppUserAddrEntity> addrEntityList = userAddrService.queryPageForApp(page_start, page_size, userToken, userId);

		Map<String, Object> map = new HashMap<>();
		map.put("addrEntityList", addrEntityList);

		logger.info("getUserAddrList方法执行结束：返回值为   "+ JSON.toJSONString(map));

		return R.ok(map);
	}

	/**
	 * 获取用户地址详情
	 */
	@Login
	@GetMapping("getUserAddrInfo")
	@ApiOperation("用户地址详情")
	public R getUserAddrInfo(@RequestParam String id, @RequestParam String token) {

		logger.info("getUserAddrInfo方法执行开始：传入参数为   "+id+"---"+token);

		String addrId = id==null?"":id;
		String userToken = token==null?"":token;

		// 用户地址
		AppUserAddrEntity addrEntity = userAddrService.queryAddrInfo(addrId, userToken);

		Map<String, Object> map = new HashMap<>();
		map.put("addrEntity", addrEntity);

		logger.info("getUserAddrInfo方法执行结束：返回值为   "+ JSON.toJSONString(map));

		return R.ok(map);
	}

	/**
	 * 添加收货地址
	 */
	@Login
	@PostMapping("addUserAddr")
	@ApiOperation("添加收货地址")
	public R addUserAddr(@RequestBody AppUserAddrEntity userAddrEntity, @RequestParam String token) {

		logger.info("addUserAddr方法执行开始：传入参数为   "+JSON.toJSONString(userAddrEntity)+"---"+token);

		String userToken = token==null?"":token;

		// 用户地址
		int num = userAddrService.addUserAddr(userAddrEntity);

		Map<String, Object> map = new HashMap<>();
		map.put("num", num);

		logger.info("addUserAddr方法执行结束：返回值为   "+ JSON.toJSONString(map));

		return R.ok(map);
	}

	/**
	 * 删除收货地址
	 */
	@Login
	@PostMapping("delUserAddr")
	@ApiOperation("删除收货地址")
	public R delUserAddr(@RequestParam String id, @RequestParam String token) {

		logger.info("delUserAddr方法执行开始：传入参数为   "+id+"---"+token);

		String addrId = id==null?"":id;
		String userToken = token==null?"":token;

		// 用户地址
		int num = userAddrService.delUserAddr(id);

		Map<String, Object> map = new HashMap<>();
		if(num>0){
			map.put("msg", "删除成功");
		}else{
			map.put("msg", "删除失败");
		}
		map.put("num", num);

		logger.info("delUserAddr方法执行结束：返回值为   "+ JSON.toJSONString(map));

		return R.ok(map);
	}

	/**
	 * 修改收货地址
	 */
	@Login
	@PostMapping("updUserAddr")
	@ApiOperation("修改收货地址")
	public R updUserAddr(@RequestBody AppUserAddrEntity userAddrEntity, @RequestParam String token) {

		logger.info("updUserAddr方法执行开始：传入参数为   "+token);

		// 用户地址
		int num = userAddrService.updUserAddr(userAddrEntity);

		Map<String, Object> map = new HashMap<>();
		if(num>0){
			map.put("msg", "更新成功");
		}else{
			map.put("msg", "更新失败");
		}
		map.put("num", num);

		logger.info("updUserAddr方法执行结束：返回值为   "+ JSON.toJSONString(map));

		return R.ok(map);
	}

	/** 
	 * 设置默认地址 TODO
	 * @author HObOCat
	 * @date 2019/11/11 15:59
	 * @return 
	 */
	@Login
	@PostMapping("defaultAddr")
	@ApiOperation("设置默认地址")
	public R defaultAddr(@RequestParam String addr_id, @RequestParam String token, @RequestParam String user_id) {

		logger.info("defaultAddr方法执行开始：传入参数为   " + addr_id + "---" +token+"---"+user_id);

		String addrId = addr_id==null?"":addr_id;
		String userId = user_id==null?"":user_id;
		String userToken = token==null?"":token;

		//更新默认地址
		int num = userAddrService.upDefaultAddr(addrId, userId, userToken);

		Map<String, Object> map = new HashMap<>();
		if(num>0){
			map.put("msg", "更新成功");
		}else{
			map.put("msg", "更新失败");
		}
		map.put("num", num);

		logger.info("defaultAddr方法执行结束：返回值为   "+ JSON.toJSONString(map));

		return R.ok(map);
	}
	
	/** 
	 * 获取用户积分流水列表
	 * @author HObOCat
	 * @date 2019/11/11 16:30
	 * @return 
	 */
	@Login
	@GetMapping("getUserScoreList")
	@ApiOperation("获取用户积分流水列表")
	public R getUserScoreList(@RequestParam String page_start, @RequestParam String page_size, @RequestParam String token,
							 @RequestParam String user_id) {

		logger.info("getUserScoreList方法执行开始：传入参数为   "+page_start+"---"+page_size+"---"+token+"---"+user_id);

		String userToken = token==null?"":token;
		String userId = user_id==null?"":user_id;

		Integer pageIndex = Integer.valueOf(page_start);
		Integer pageSize = page_size == null ? 20 : Integer.valueOf(page_size);

		// 获取用户积分流水信息
		List<AppUserScoreEntity> scoreEntityList = userScoreService.queryPageForApp(pageIndex, pageSize, userToken, userId);

		Map<String, Object> map = new HashMap<>();
		map.put("scoreEntityList", scoreEntityList);

		logger.info("getUserScoreList方法执行结束：返回值为   "+ JSON.toJSONString(map));

		return R.ok(map);

	}

}
